const AdminModel = require('../model/admin')
const Constant = require('../constant/constant')
const Common = require('./common')
const Sequelize = require('sequelize')
const dateFormat = require('dateformat')
const Op = Sequelize.Op
let resObj = {
    list,
    update,
    remove
}
module.exports = resObj
function list (req,res){
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams:(cb) =>{
            Common.checkParams(req.query,['page','rows'],cb)
        },
        query:['checkParams',(results,cb)=>{
            let offset = req.query.rows*(req.query.page - 1)||0;
            let limit = parseInt(req.query.rows)||20;
            let whereCondition = {};
            if(req.query.username){
                whereCondition.username = {
                    [Op.like]:req.query.username+'%'
                }
            }
            AdminModel.findAndCountAll({
                where:whereCondition,
                offset:offset,
                limit:limit,
                order:[['created_at','DESC']],
            }).then((res)=>{
                let list = [];
                res.rows.forEach((v,i)=>{
                    let obj = {
                        id:v.id,
                        username:v.username,
                        name:v.name,
                        role:v.role,
                        lastLoginAt:dateFormat(v.lastLoginAt,'yyyy-mm-dd HH:MM:ss'),
                        createdAt:dateFormat(v.createdAt,'yyyy-mm-dd HH:MM:ss'),
                    };
                    list.push(obj);
                })
                resObj.data={
                    list,
                    count:res.count
                }
                cb(null)
            }).catch((err)=>{
                console.log(err);
                cb(Constant.DEFAULT_ERROR)
            })
        }]
    }
    Common.autoFn(tasks,res,resObj)
}
function update(req,res){
    console.log(req.body);
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams:(cb)=>{
            Common.checkParams(req.body,['id','username','name','role'],cb);
        },
        update:['checkParams',(results,cb)=>{
            AdminModel.update({
            username:req.body.username,
            name:req.body.name,
            role:req.body.role
           },{
               where:{
                   id:req.body.id
               }
           }).then((res)=>{
               if(res[0]){
                   cb(null)
               }else{
                   cb(Constant.WISH_NOT_EXSIT)
               }
           }).catch((err)=>{
               console.log(err);
               cb(Constant.DEFAULT_ERROR)
           })
        }]
    };
    Common.autoFn(tasks,res,resObj)
}
function remove(req,res){
    console.log(req.body);
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams:(cb)=>{
            Common.checkParams(req.body,['id'],cb);
        },
        remove:['checkParams',(results,cb)=>{
            AdminModel.destroy({
               where:{
                   id:req.body.id
               }
           }).then((res)=>{
               if(res){
                   cb(null)
               }else{
                   cb(Constant.WISH_NOT_EXSIT)
               }
           }).catch((err)=>{
               console.log(err);
               cb(Constant.DEFAULT_ERROR)
           })
        }]
    };
    Common.autoFn(tasks,res,resObj)
}